package com.supermap.dataservice.cloud.cropland;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description 农田关联的农情建议咨询灾情
 * @date 2018年11月14 上午 9:21
 * @email 1301584033@qq.com
 */
@Mapper
public interface CroplandMapper {
    /**
     *
     * 农田相关的农情
     */
    @SelectProvider(type = CroplandMapperSQL.class,method ="getFarmLandByIdSQL" )
    List<Map<String,Object>>  getFarmLandById(@Param("farmId")String farmId,@Param("pageNum")String pageNum,@Param("pageSize")String pageSize);

    /**
     *
     * 农田相关农田咨询
     */
    @SelectProvider(type = CroplandMapperSQL.class,method ="getAdvFarmByIdSQL" )
    List<Map<String,Object>>  getAdvFarmById(@Param("farmId")String farmId,@Param("pageNum")String pageNum,@Param("pageSize")String pageSize);


    /**
     *
     * 农田相关灾情
     */
    @SelectProvider(type = CroplandMapperSQL.class,method ="getDisasterFarmByIdSQL" )
    List<Map<String,Object>>  getDisasterFarmById(@Param("farmId")String farmId,@Param("pageNum")String pageNum,@Param("pageSize")String pageSize);





    class CroplandMapperSQL{


        public String getFarmLandByIdSQL(Map<String,Object> map){
            String farmId=(String)map.get("farmId");
            String pageNum=(String)map.get("pageNum");
            String pageSize=(String) map.get("pageSize");
            StringBuilder stringBuilder =new StringBuilder();
            stringBuilder.append("select t2.username,t2.showName,t1.descript,date_format(t1.createtime,'%Y-%m-%d %H:%i:%S')  as  createtime from t_agrinfo t1\n" +
                    "inner join  t_user t2\n" +
                    "on t1.userId=t2.id ");
            if(farmId!=null){
                stringBuilder.append("and t1.farm="+farmId+" ");
            }
            stringBuilder.append("order by t1.createtime DESC ");
            if(pageNum!=null && pageSize!=null){
                stringBuilder.append(" LIMIT "+ (Integer.valueOf(pageNum)-1)*Integer.valueOf(pageSize)+","+pageSize+"");
            }
            return  stringBuilder.toString();

        }
        public String getAdvFarmByIdSQL(Map<String,Object> map){
            String farmId=(String)map.get("farmId");
            String pageNum=(String)map.get("pageNum");
            String pageSize=(String) map.get("pageSize");
            StringBuilder stringBuilder =new StringBuilder();
            stringBuilder.append("select t2.username,t2.showName,t1.question,date_format(t1.uploadTime,'%Y-%m-%d %H:%i:%S')  as  createtime from t_advisoryinfo t1\n" +
                    "inner join  t_user t2\n" +
                    "on t1.userId=t2.id ");
            if(farmId!=null){
                stringBuilder.append("and t1.farmlandid="+farmId+" ");
            }
            stringBuilder.append(" order by t1.uploadTime desc");

            if(pageNum!=null && pageSize!=null){
                stringBuilder.append(" LIMIT "+ (Integer.valueOf(pageNum)-1)*Integer.valueOf(pageSize)+","+pageSize+"");
            }
            return  stringBuilder.toString();
        }

        public String getDisasterFarmByIdSQL(Map<String,Object> map){
            String farmId=(String)map.get("farmId");
            String pageNum=(String)map.get("pageNum");
            String pageSize=(String) map.get("pageSize");
            StringBuilder stringBuilder =new StringBuilder();
            stringBuilder.append("select t2.username,t2.showName,t1.descript,t3.name,t1.development,date_format(t1.createtime,'%Y-%m-%d %H:%i:%S')  as  createtime from t_disaster t1\n" +
                    "inner join  t_user t2\n" +
                    "on t1.userId=t2.id ");
            if(farmId!=null){
                stringBuilder.append("and t1.farm="+farmId+" ");
            }
            stringBuilder.append(" Left join t_disastertype t3\n" +
                    "on t1.disastertype =t3.id\n" +
                    "order by t1.createtime desc ");
            if(pageNum!=null && pageSize!=null){
                stringBuilder.append(" LIMIT "+ (Integer.valueOf(pageNum)-1)*Integer.valueOf(pageSize)+","+pageSize+"");
            }
            return  stringBuilder.toString();
        }



    }

}
